import * as React from 'react';
import "../less/ILogin.less";
import IForm from '../components/IForm';
import http from '../utils/http';
import { Form } from 'antd';
import { WrappedFormUtils } from 'antd/lib/form/Form';
import store from '../store/index';
import { title } from "../../config/config"
import IButton from '../components/IButton';
import validation from '../utils/validation';
import * as moment from 'moment';
console.log(moment);

interface Props {
    form?: WrappedFormUtils,
    history?: any,
}


const Login: React.FC<Props> = (props: Props) => {

    const IFormItems = [{
        type: "text",
        name: "email",
        label: "账号",
        validation: validation.required,
    }, {
        type: "password",
        name: "password",
        label: "密码",
        validation: validation.required,
    }];

    const IFormData = {
        // email: "ibinz11",
        // password: moment('2015-01-01', 'YYYY-MM-DD')
    }

    const handleSubmit = async () => {
        const value = await validation.validateFields(props.form);
        const { data } = await http.post("/login", value);
        store.userStore.login(data.data);
        props.history.replace("/");
    }

    return (
        <div className="login-form">
            <div className="iTitle center">{title}</div>
            <IForm
                {...props}
                items={IFormItems}
                data={IFormData}
            />
            <IButton name="登录"type="primary" onClick={handleSubmit} />
        </div>
    )
}

export default Form.create({ name: 'Login' })(Login);